package com.unistec.fileserver.server;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchAutoConfiguration;
import org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchDataAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

import com.github.vanroy.springboot.autoconfigure.data.jest.ElasticsearchJestAutoConfiguration;
import com.hnit.auth.client.EnableAceAuthClient;

/**
 * 文件服务器启动类
 * 
 * @description
 * @author 胡晓光
 * @date 2021-9-29
 * @time 10:03:54
 *
 */
@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class, ElasticsearchAutoConfiguration.class, ElasticsearchDataAutoConfiguration.class, ElasticsearchJestAutoConfiguration.class })
@EnableFeignClients({ "com.hnit.auth.client.feign", "com.hnit.api.feign", "com.unistec.fileserver.server.mapper" })
@EnableScheduling
@EnableEurekaClient
@EnableAceAuthClient
@EnableCircuitBreaker
@MapperScan({ "com.unistec.fileserver.server.mapper" })
@ComponentScan(basePackages = { "com.unistec.fileserver.server", "com.unistec.framework", "com.unistec.taskcenter.client" })
public class FileServerBootstrap {
	public static void main(String[] args) {
		SpringApplication.run(FileServerBootstrap.class, args);
	}

	/**
	 * 配置跨域请求
	 * 
	 * @description
	 * @author 胡晓光
	 * @date 2021-9-29
	 * @time 10:03:44
	 * 
	 * @return
	 */
	private CorsConfiguration buildConfig() {
		CorsConfiguration corsConfiguration = new CorsConfiguration();
		corsConfiguration.addAllowedOrigin("*");
		corsConfiguration.addAllowedHeader("*");
		corsConfiguration.addAllowedMethod("*");
		corsConfiguration.setAllowCredentials(true);// 这两句不加不能跨域上传文件，
		corsConfiguration.setMaxAge(3600l);// 加上去就可以了
		return corsConfiguration;
	}

	/**
	 * 配置支持跨域请求
	 * 
	 * @description
	 * @author 胡晓光
	 * @date 2021-9-29
	 * @time 10:03:28
	 * 
	 * @return
	 */
	@Bean
	public CorsFilter corsFilter() {
		UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
		source.registerCorsConfiguration("/**", buildConfig()); // 4
		return new CorsFilter(source);
	}

	/**
	 * 配置RestTemplate
	 * 
	 * @description
	 * @author 胡晓光
	 * @date 2021-9-29
	 * @time 10:03:36
	 * 
	 * @return
	 */
	@Bean
	@LoadBalanced
	public RestTemplate restTemplate() {
		return new RestTemplate();
	}
}
